package com.compass.doctor.ui

import androidx.lifecycle.MutableLiveData
import com.compass.framework.ext.toJson
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.AllDictionariesWarp
import com.compass.framework.model.Dictionaries
import com.compass.framework.model.TodayNumber
import com.compass.framework.model.UpdateInfo
import com.compass.framework.model.User
import com.compass.framework.toast.TipsToast
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.netease.nimlib.sdk.StatusCode
import com.netease.nimlib.sdk.auth.OnlineClient

/**
 * Description： app主页ViewModel
 * Date:2023/11/24 15:42
 */
class MainViewModel : BaseViewModel() {
    val dictionariesLiveData = MutableLiveData<MutableList<Dictionaries>?>()
    val allDictionariesLiveData = MutableLiveData<AllDictionariesWarp?>()
    val patientLiveData = MutableLiveData<Int>()
    val todayNumberLiveData = MutableLiveData<TodayNumber?>()
    val versionLiveData = MutableLiveData<UpdateInfo?>()
    val signOutLiveData = MutableLiveData<Any?>()

    /**
     * 其它第三方登录的设备
      */
    val onlineOtherClientLiveData = MutableLiveData<OnlineClient>()

    /**
     * 当前用户IM登录的状态
     */
    val imLoginStateLiveData = MutableLiveData<StatusCode>()

    /**
     * 今日新增已接诊、待接诊数量
     */
    fun orderTodayCount() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.orderTodayCount()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {

            }
        }, successBlock = {
            todayNumberLiveData.value = it
        })
    }

    /**
     * 注销
     */
    fun signOut() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.signOut()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                signOutLiveData.value=null
            }
        }, successBlock = {
            signOutLiveData.value = it
        })
    }


    fun getAppVersion() {
        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.getAppVersion("1")
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
            }
        }, successBlock = {
            versionLiveData.value = it
        })
    }


    /**
     * 获取全部字典缓存
     */
    fun getAllDictionaries() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getDictionaries()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            allDictionariesLiveData.value = it

        })
    }


    //获取医生信息
    val userLiveData = MutableLiveData<User?>()

    fun getUserInfo() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getUserInfo()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                userLiveData.value = null
            }
        }) {
            userLiveData.value = it
        }
    }


}